<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
        <el-form-item label="所属客户" prop="strCustomer">
          <treeselect style="width: 300px;" v-model="queryParams.strCustomer" :show-count="true"
                      :multiple="true"
                      :disable-branch-nodes="true"
                      @keyup.enter.native="handleQuery"
                      :options="customerTree"  placeholder="请输入所属客户" />
        </el-form-item>
        <el-form-item label="创建时间">
          <el-date-picker
            v-model="dateRange"
            style="width: 240px"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="['00:00:00', '23:59:59']"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                        type="primary"
                        plain
                        icon="el-icon-plus"
                        size="mini"
                        @click="handleAdd"
                        v-hasPermi="['reportworld:record:add']"
                >新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="success"
                        plain
                        icon="el-icon-edit"
                        size="mini"
                        :disabled="single"
                        @click="handleUpdate"
                        v-hasPermi="['reportworld:record:edit']"
                >修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="danger"
                        plain
                        icon="el-icon-delete"
                        size="mini"
                        :disabled="multiple"
                        @click="handleDelete"
                        v-hasPermi="['reportworld:record:remove']"
                >删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="warning"
                        plain
                        icon="el-icon-download"
                        size="mini"
                        @click="handleExport"
                        v-hasPermi="['reportworld:record:export']"
                >导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

      <el-table border="border" max-height="550px" v-loading="loading" :data="recordList"
                @selection-change="handleSelectionChange" @row-click="rowClick"
                @sort-change="handleSortChange" :row-style="tableRowStyle" ref="record">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="操作" align="center" width="100" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleUpdate(scope.row,false)"
              v-hasPermi="['reportworld:record:query']"
            >详情
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="所属客户" width="180" align="center" prop="customerName" :show-overflow-tooltip="true"
                         sortable="custom" :sort-orders="['descending', 'ascending']"/>
        <el-table-column label="文件名称" width="180" align="center" prop="fileName" :show-overflow-tooltip="true" sortable="custom"
                         :sort-orders="['descending', 'ascending']"/>
        <el-table-column label="说明" width="180" align="center" prop="description" :show-overflow-tooltip="true" sortable="custom"
                         :sort-orders="['descending', 'ascending']"/>
        <el-table-column label="版本号" width="180" align="center" prop="version">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.report_version" :value="scope.row.version"/>
          </template>
        </el-table-column>
        <el-table-column label="添加时间" align="center" prop="createTime" width="180" sortable="custom" :sort-orders="['descending', 'ascending']">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="添加人" align="center" width="150" prop="createBy" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']"/>
        <el-table-column label="编辑时间" align="center" prop="updateTime" width="180" sortable="custom" :sort-orders="['descending', 'ascending']">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updateTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="编辑人" align="center" width="150" prop="updateBy" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']"/>
        <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']"/>
      </el-table>

        <pagination
                v-show="total>0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
        />

        <!-- 添加或修改版本记录信息对话框 -->
      <el-dialog :title="title" :close-on-click-modal ="false" :visible.sync="open" width="1200px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="所属客户" prop="customerId">
                <treeselect  v-model="form.customerId"
                             :show-count="true"
                             :multiple="false"
                             :disable-branch-nodes="true"
                             :options="customerTree"
                             placeholder="请选择所属客户"/>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="文件名称" prop="fileName">
                <textarea  v-model="form.fileName" style="width:100%;height: 80px" placeholder="请输入文件名称"></textarea>
                <span slot="label">
                <el-tooltip content="填写jar包名称，采集EXECL,规则EXECL,部署包，SQL脚本等，例如：XXX.jar" placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                文件名称
              </span>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="说明" prop="description">
                <textarea  v-model="form.description" style="width:100%;height: 110px" placeholder="请输入说明"></textarea>
                <span slot="label">
                <el-tooltip content="填写解决了现场的现场问题描述，例如：发送的SQL脚本解决现场XXX页面报错的问题" placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                说明
              </span>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <textarea  v-model="form.remark" style="width:100%;height: 100px" placeholder="请输入备注"></textarea>
                <span slot="label">
                <el-tooltip content="备注信息可以填写经过谁同意发送，或者其它备注信息，例如：XXX同意直接发送给现场" placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                备注
              </span>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item v-if="!isUpdate" label="客户信息" prop="customerInfo" >
                <textarea  v-model="form.customerInfo"  style="width:100%;height: 100px" ></textarea>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="版本号" prop="version">
                <el-select clearable v-model="form.version" placeholder="请选择版本号">
                  <el-option
                    v-for="dict in dict.type.report_version"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" v-if="isUpdate" :loading="butloading" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
</template>

<script>
    import { listRecord, getRecord, delRecord, addRecord, updateRecord,init } from "@/api/reportworld/record";
    import { getTableRowStyle } from "@/utils/yss";
    import Treeselect from "@riophae/vue-treeselect";
    import "@riophae/vue-treeselect/dist/vue-treeselect.css";
    export default {
        name: "Record",
        components: {
          Treeselect
        },
            dicts: ['report_version'],
        data() {
            return {
              butloading:false,
              isUpdate: false,
                // 遮罩层
                loading: true,
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 版本记录信息表格数据
                recordList: [],
              // 日期范围
              dateRange: [],
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
              // 所属客户
              customerTree: [],
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 50,
                    fileName: null,
                   strCustomer: null,
                    demandBugNum: null,
                    gitVersion: null,
                    sceneContactBy: null,
                    databaseType: null,
                    databaseVersion: null,
                    serverType: null,
                    serverVersion: null,
                    description: null,
                    remark: null
        },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
              customerId: [
                { required: true, message: "所属客户不能为空", trigger: "change" }
              ],
              description: [
                { required: true, message: "说明不能为空", trigger: "change" }
              ],
            }
        };
        },
        created() {
            this.init();
            this.getList();
        },
        methods: {
          init(){
            init().then(response => {
              this.customerTree=response.customerTree;
          });
          },
            /** 查询现场对接记录信息列表 */
            getList() {
                this.loading = true;
                listRecord(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
                 this.recordList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                        id: null,
                        fileName: null,
                        demandBugNum: null,
                        gitVersion: null,
                        sceneContactBy: null,
                        databaseType: null,
                        databaseVersion: null,
                        serverType: null,
                        serverVersion: null,
                        description: null,
                        createTime: null,
                        createBy: null,
                        updateTime: null,
                        updateBy: null,
                        remark: null
            };
                this.resetForm("form");
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.dateRange = [];
                this.resetForm("queryForm");
                this.handleQuery();
            },
            // 多选框选中数据
            handleSelectionChange(selection) {
                this.ids = selection.map(item => item.id)
                this.single = selection.length!==1
                this.multiple = !selection.length
            },
            /** 排序触发事件 */
            handleSortChange(column, prop, order) {
                this.queryParams.orderByColumn = column.prop;
                if("customerName"== column.prop){
                  this.queryParams.orderByColumn = "b.name";
                }
                this.queryParams.isAsc = column.order;
                this.getList();
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.reset();
                this.open = true;
                this.isUpdate =true;
                this.title = "添加版本记录信息";
                this.form.version = this.dict.type.report_version[0].value;
            },
            /** 修改按钮操作 */
            handleUpdate(row,isUpdate) {
                this.reset();
                const id = row.id || this.ids
                getRecord(id).then(response => {
                    this.form = response.data;
                this.open = true;
                this.title = "修改版本记录信息";
              this.isUpdate =true;
              if(isUpdate == false){
                this.title = "版本记录信息详情";
                this.isUpdate =false;
              }
            });
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                      this.butloading=true;
                        if (this.form.id != null) {
                            updateRecord(this.form).then(response => {
                                this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        }).finally(() => {this.butloading=false});
                        } else {
                            addRecord(this.form).then(response => {
                                this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        }).finally(() => {this.butloading=false});
                        }
                    }
                });
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                const ids = row.id || this.ids;
                this.$modal.confirm('是否确认删除数据项？').then(function() {
                    return delRecord(ids);
                }).then(() => {
                    this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => {});
            },
    /** 导出按钮操作 */
    handleExport() {
        this.download('reportworld/record/export', {
                ...this.queryParams
    }, `record_${new Date().getTime()}.xlsx`)
    },
    tableRowStyle({ row, rowIndex}){
        return getTableRowStyle(row,rowIndex,this.ids);
    },
    /** 单击选中行数据 */
    rowClick(row) {
        this.$refs.record.toggleRowSelection(row);
    }
    }
    };
</script>
